@extends('admin.tableLayout')
@section('title','权限管理（后台）')
@section('quote')
    <button type="button" class="layui-btn layui-btn-small" id="getAll" onclick="javascript:location.href='{{route('admin.permission-add')}}'"><i class="fa fa-plus" aria-hidden="true"></i> 添加权限</button>
    <hr/>
    <form class="layui-form" >
        <form class="layui-form" >
            <label>组名：</label>
            <div class="layui-inline">
                <select name="group_name"  id="group_name" lay-search >
                    <option value="">全部</option>
                    @foreach($group as $k=>$v)
                        <option value="{{$v}}">{{$v}}</option>
                    @endforeach
                </select>

            </div>
            <label>组名：</label>
            <div class="layui-inline">
                <select name="guard_name"  id="guard_name" lay-search >
                    <option value="">全部</option>
                    @foreach($guard as $k=>$v)
                        <option value="{{$v}}">{{$v}}</option>
                    @endforeach
                </select>

            </div>
            <button lay-filter="search" class="layui-btn" id="search"><i class="fa fa-search" aria-hidden="true"></i> 查询</button>
        </form>
    @endsection
@section('toolbar')
    @can('admin')
        <a class="layui-btn layui-btn-xs " lay-event="edit"><i class="fa fa-edit"></i> 编辑</a>
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete"><i class="fa fa-trash-o"></i> 删除</a>

    @endcan
@endsection
@section('tableRender')
    <script>
        layui.use(['table','jquery','form'], function(){
            var table = layui.table;
            var $=layui.jquery;
            var form=layui.form;
            var _renderLoad = layer.msg('正在生成权限，请稍候',{icon: 16,time:false,shade:0.8});
            ;
            var render=table.render({
                elem:"#table",
                page:true,
                id:'idTest',
                url:'{{route('admin.permissionAjax')}}',
                where:{
                    group_name:$('#group_name').val(),
                    guard_name:$('#guard_name').val()
                },
                height:400,
                cols:  [[ //标题栏
                    {checkbox:true,fixed:true},
                    {field: 'name', title: '权限名称', width: 200},
                    {field: 'desc', title: '描述', width: 200},
                    {field: 'group_name', title: '所属组', width: 200},
                    {field:'guard_name',title:'监听组',width:100},
                    {fixed: 'right', width:350,title:'操作', align:'center', toolbar: '#toolbar'}
                    //这里的toolbar值是模板元素的选择器
                ]],
                response: {//返回格式自定义
                    statusName: 'code' //数据状态的字段名称，默认：code
                    ,statusCode: 0 //成功的状态码，默认：0
                    ,msgName: 'msg' //状态信息的字段名称，默认：msg
                    ,countName: 'total' //数据总数的字段名称，默认：count
                    ,dataName: 'data' //数据列表的字段名称，默认：data
                },
                done:function () {
                    layer.close(_renderLoad);
                }
            });
            table.on('checkbox(table)', function(obj){
                //console.log(obj)
            });
            $('#search').on('click',function () {
                var data = {
                    group_name:$('#group_name').val(),
                    guard_name:$('#guard_name').val()
                };
                _renderLoad = layer.msg('正在生成权限，请稍候',{icon: 16,time:false,shade:0.8});
                render.reload(
                    {
                        where:data
                    }
                );
                return false;
            })
            table.on('tool(table)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值
                var tr = obj.tr; //获得当前行 tr 的DOM对象
                if(layEvent === 'delete'){ //删除
                    layer.confirm('真的删除行么', function(index){
                        layer.close(index);
                        //向服务端发送删除指令
                        $.get('{{route('admin.permission',['act'=>'delete'])}}',{id:data.id},function(e){
                            if(e.info=='success'){
                                layer.msg('操作成功',{icon:1});
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                            }
                        });
                    });
                } else if(layEvent === 'edit'){ //编辑
                    location.href='/admin/permission/edit/'+data.id;
                    //do something
                    //同步更新缓存对应的值
                }
            });
        });

    </script>
@endsection